package com.palmgo.utils.drawer.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.zip.Inflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
	/**
	 * 
	 * @param src
	 *            需要压缩的原文件
	 * @param dst
	 *            压缩文件的存放路径
	 * @return
	 */
	public static boolean zip(File src, File dst) {
		ZipOutputStream out = null;
		try {
			if (!src.exists()) {
				return false;
			}
			out = new ZipOutputStream(new FileOutputStream(dst));
			if (dst.isFile()) {

			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private static void writeZipToFile(ZipOutputStream output, File f) {

	}

	// public static boolean unZip(ZipFile src, File outputFile) {
	//
	// }
	public static boolean unZip(ZipFile src, File outputFile) {
		try {
			Enumeration e = src.entries();
			ZipEntry entry = null;
			outputFile.mkdirs();
			while (e.hasMoreElements()) {
				entry = (ZipEntry) e.nextElement();
				String entryName = entry.getName();
				try {
					if (entry.isDirectory()) {
						File item = new File(outputFile, entryName);
						item.mkdirs();
					}
					writeStream(src.getInputStream(entry), new File(outputFile,
							entryName));
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static void writeStream(InputStream is, File dst) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(dst);
			int len = -1;
			byte[] b = new byte[1024];
			while ((len = is.read(b)) != -1) {
				fos.write(b, 0, len);
			}
			fos.flush();
		} catch (Exception e) {
		} finally {
			try {
				is.close();
				fos.close();
			} catch (Exception e2) {
			}
		}
	}

	public static String unZip(String str) {
		try {
			Inflater mInflater = new Inflater();
			mInflater.setInput(str.getBytes(), 0, str.getBytes().length);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			int len = -1;
			byte[] buf = new byte[1024];
			while (!mInflater.finished()) {
				mInflater.inflate(buf);
				bos.write(buf, 0, len);
			}
			str = new String(bos.toByteArray());
			bos.close();
			return str;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/***
	 * 
	 * 解压Zip数据为String
	 * 
	 * @param data
	 * @return
	 */
	public static String unZipByteToString(byte[] data) {
		byte[] result = unZipByte(data);
		String outputString = null;
		try {
			outputString = new String(result, 0, result.length, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return outputString;
	}

	public static byte[] unZipByte(byte[] data) {
		Inflater decompresser = new Inflater();
		decompresser.setInput(data);
		byte result[] = new byte[0];
		ByteArrayOutputStream o = new ByteArrayOutputStream(1);
		try {
			byte[] buf = new byte[1024];
			int got = 0;
			while (!decompresser.finished()) {
				got = decompresser.inflate(buf);
				o.write(buf, 0, got);
			}
			result = o.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				o.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			decompresser.end();
		}
		return result;
	}

	/**
	 * 解压字符串<br>
	 * 前提：压缩包中只有一个文件<br>
	 * 由于老版本无法正常解析，故添无用参数
	 *
	 * @return
	 */
	public static String unZip(byte[] bytes) {
		try {
			if (bytes.length == 0) {
				return "";
			}
			ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(
					bytes));
			ZipEntry entry = zis.getNextEntry();
//			if (entry != null && !entry.isDirectory()) {
//				ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
//				byte[] buf = new byte[4096];
//				int len = -1;
//				while ((len = zis.read(buf)) != -1) {
//					buffer.append(buf, 0, len);
//				}
//				return new String(buffer.toByteArray());
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}
